package Utils;

import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张
 * Date: 2025-01-22
 * Time: 15:17
 */
//文件操作的封装类
public class FileUtil {
    //负责把 filePath 对应的文件内容读取出来
    public static String readFile(String filePath) {
        //只是在修改局部变量,与多线程无关,无需使用到StringBuffer
        StringBuilder result = new StringBuilder();
        try(FileReader fileReader = new FileReader(filePath)) {
            while(true) {
                int ch = fileReader.read();//返回值为int(一个字符)
                if(ch == -1) {
                    break;
                }
                result.append((char)ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result.toString();
    }

    //负责把content写入到filePath对应的文件中
    public static void writerFile(String filePath,String content) {
        try(FileWriter fileWriter = new FileWriter(filePath)) {
            fileWriter.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        FileUtil.writerFile("d:/test.txt","hello word");
//        String content= FileUtil.readFile("d:/test.txt");
//        System.out.println(content);
//    }
}
